package com.photographysharing.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.photographysharing.common.StatisticOperation;
import com.photographysharing.entity.Statistics;
import com.photographysharing.exception.ServiceException;
import com.photographysharing.service.StatisticsService;
import com.photographysharing.mapper.StatisticsMapper;
import com.photographysharing.util.RedisHelp;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.Map;

/**
* @author Z
* @description 针对表【statistics】的数据库操作Service实现
* @createDate 2024-05-14 21:15:12
*/
@Service
public class StatisticsServiceImpl extends ServiceImpl<StatisticsMapper, Statistics>
    implements StatisticsService{
    private RedisHelp redisHelp;
    private String name;
    private StatisticsMapper mapper;

    public StatisticsServiceImpl(RedisHelp redisHelp, StatisticsMapper mapper) {
        this.redisHelp = redisHelp;
        this.mapper = mapper;
        name = "statistics";
    }

    @Override
    public void initstatistics(String photoId) {
        try {
            Statistics statistics = new Statistics(photoId);
            String key = name+photoId;
            redisHelp.setObject(key,statistics);
        }catch (Exception e)
        {

        }

    }
    public void updateStatistics(String photoId, StatisticOperation operation) {
        try{
            String json = redisHelp.getString(name + photoId);
            Statistics statistics;
            if (json == null || json == "OK") {
                QueryWrapper<Statistics> qw = new QueryWrapper<>();
                qw.eq("photo_id", photoId);
                statistics = mapper.selectOne(qw);
            } else {
                statistics = JSONUtil.toBean(json, Statistics.class);
            }
            if (statistics != null) {
                operation.perform(statistics);
                if (mapper.updateById(statistics) > 0) {
                    redisHelp.removeKey(name + photoId);
                    redisHelp.setObject(name + photoId, statistics);
                }
            }
        }catch (Exception e){
            throw new ServiceException("系统错误!");
        }
    }

    @Override
    public void increaseLike(String photoId) {
        updateStatistics(photoId, Statistics::increaseLike);
    }

    @Override
    public void reduceLike(String photoId) {
        updateStatistics(photoId, Statistics::reduceLike);
    }
    @Override
    public void increaseWatch(String photoId) {
        updateStatistics(photoId, Statistics::increaseWatch);
    }

    @Override
    public Statistics getstatistics(String photoId) {
       try{
           String json = redisHelp.getString(name+photoId);
           if(json ==null){
               QueryWrapper<Statistics> qw = new QueryWrapper<>();
               qw.eq("photo_id",photoId);
               Statistics statistics = mapper.selectOne(qw);
               if(statistics !=null){
                   redisHelp.removeKey(name+photoId);
                   redisHelp.setObject(name+photoId,statistics);
               }
               return statistics;
           }else{
               Statistics statistics = JSONUtil.toBean(json, Statistics.class);
               if(statistics.getId() == null){
                   redisHelp.removeKey(name+photoId);
               }
               return statistics;
           }
       }catch (Exception e){
           throw new ServiceException("统计异常!");
       }
    }

    @Override
    public void insertstatistics(String photoId) {
        try {
            Statistics statistics = new Statistics(photoId);
            if(mapper.insert(statistics)>0){
                initstatistics(photoId);
            }
        }catch (Exception e){
            throw new ServiceException("统计异常!");
        }
    }


}




